package com.freesky.biz.abchina.service.impl;

import com.freesky.biz.abchina.domain.TMaotaiCountRecord;
import com.freesky.biz.abchina.domain.TMaotaiRecord;
import com.freesky.biz.abchina.domain.TMaotaiShare;
import com.freesky.biz.abchina.domain.TPrivateUser;
import com.freesky.biz.abchina.mapper.MaotaiRecordMapper;
import com.freesky.biz.abchina.mapper.PrivateUserMapper;
import com.freesky.biz.abchina.service.IMaotaiRecordService;
import com.freesky.biz.abchina.service.IPrivateUserService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaotaiRecordServiceImpl implements IMaotaiRecordService {

    @Autowired
    private MaotaiRecordMapper maotaiRecordMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TMaotaiRecord> findList(TMaotaiRecord entity) {
        return maotaiRecordMapper.findList(entity);
    }

    @Override
    public AjaxResult exportFile(TMaotaiRecord entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("MAOTAI_RECORD");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TMaotaiRecord(), IMaotaiRecordService.class,
                "findList", "抢茅台",Long.parseLong(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

    @Override
    public List<TMaotaiShare> findShareList(TMaotaiShare entity) {
        return maotaiRecordMapper.findShareList(entity);
    }

    @Override
    public AjaxResult exportShareFile(TMaotaiShare entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("MAOTAI_SHARE");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TMaotaiShare(), IMaotaiRecordService.class,
                "findShareList", "抢茅台分享",Long.parseLong(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

    @Override
    public List<TMaotaiCountRecord> findMaotaiCountList(TMaotaiCountRecord entity) {
        return maotaiRecordMapper.findMaotaiCountList(entity);
    }

    @Override
    public AjaxResult exportMaotaiCount(TMaotaiCountRecord entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("MAOTAI_COUNT");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TMaotaiCountRecord(), IMaotaiRecordService.class,
                "findMaotaiCountList", "统计信息",Long.parseLong(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

}
